package social.hub.linkedin;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import social.hub.util.UrlBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.ClientFilter;
import com.sun.jersey.api.client.filter.LoggingFilter;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.oauth.client.OAuthClientFilter;
import com.sun.jersey.oauth.signature.OAuthParameters;
import com.sun.jersey.oauth.signature.OAuthSecrets;

/**
 * https://developers.facebook.com/docs/reference/api/
 * 
 * @author Andre
 */
public class LinkedInClientImpl implements LinkedInClient {

	private String	accessToken;

	private Client	client;

	private String	PROFILE_CURRENT_USER	= "http://api.linkedin.com/v1/people/~:(id,first-name,last-name,num-recommenders)";
	private String	PROFILE_PUBLICATIONS	= "http://api.linkedin.com/v1/people/~:(publications:(id,title,date,summary))";

	public LinkedInClientImpl(String consumerToken, String consumerTokenSecret, String accessToken,
			String accessTokenSecret) {
		
		ClientConfig config = new DefaultClientConfig();

		Map<String, Object> props = config.getProperties();
		props.put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
		props.put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, 0);
		props.put(ClientConfig.PROPERTY_READ_TIMEOUT, 0);

		Map<String, Boolean> features = config.getFeatures();
		features.put(ClientConfig.FEATURE_DISABLE_XML_SECURITY, true);
		features.put(ClientConfig.FEATURE_FORMATTED, true);
		features.put(ClientConfig.FEATURE_XMLROOTELEMENT_PROCESSING, false);
		features.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

		client = Client.create(config);

		OAuthParameters oauthParameters = new OAuthParameters().consumerKey(consumerToken).token(
				accessToken);
		OAuthSecrets oauthSecrets = new OAuthSecrets().consumerSecret(consumerTokenSecret)
				.tokenSecret(accessTokenSecret);

		OAuthClientFilter filter = new OAuthClientFilter(client.getProviders(), oauthParameters,
				oauthSecrets);

		client.addFilter(new ClientFilter() {

			@Override
			public ClientResponse handle(ClientRequest request) throws ClientHandlerException {

				ClientResponse response = getNext().handle(request);

				List<String> list = response.getHeaders().get("content-type");

				String contentType = list.get(0);

				if (contentType.equals("text/xml;charset=UTF-8")) {
					response.getHeaders().put("content-type",
							Collections.singletonList(MediaType.APPLICATION_XML));
				}
				return response;
			}
		});

		client.addFilter(filter);
		client.addFilter(new LoggingFilter());

	}

	public Person getProfileForCurrentUser() {
		String url = new UrlBuilder(PROFILE_CURRENT_USER).addQueryParan("access_token", accessToken).toString();
		return client.resource(url).get(Person.class);
	}

	public PublicationList getPublications() {
		String url = new UrlBuilder(PROFILE_PUBLICATIONS).addQueryParan("access_token", accessToken).toString();
		return client.resource(url).get(Person.class).getPublicationList();
	}
}
